import {
    CHANGE_INPUT,
    ADD_ITEM,
    DELETE_ITEM,
    STR_CHANGE,
    SLICE_CHANGE,
    ADD_SLICEX,
    ADD_SLICEY
} from './actionTypes';


const defaultStore = {
    inputValue: 'Write Something',
    list: [
        '早上4点起床，锻炼身体',
        '中午下班游泳一小时',
        '下午学习三个小时'
    ],
    str: 'abcdefghijklmnopqrstuvwxyz',
    ind: '',
    curstr: '',
    slicex: '',
    slicey: ''
}

// eslint-disable-next-line import/no-anonymous-default-export
export default (state = defaultStore, action) => {
    // reducer里只能就收state，不能改变state
    // reducer必须是一个纯函数
    if (action.type === CHANGE_INPUT) {
        let newState = JSON.parse(JSON.stringify(state))
        newState.inputValue = action.value
        return newState
    }
    if (action.type === ADD_ITEM) {
        let newState = JSON.parse(JSON.stringify(state))
        newState.list.push(newState.inputValue)
        newState.inputValue = ''
        return newState
    }
    if (action.type === DELETE_ITEM) {
        let newState = JSON.parse(JSON.stringify(state))
        newState.list.splice(action.value, 1)
        return newState
    }
    // 字符串方法
    // indexOf()方法
    if (action.type === STR_CHANGE) {
        let newState = JSON.parse(JSON.stringify(state))
        newState.ind = newState.str.indexOf(action.value)
        return newState
    }
    // slice()方法
    if (action.type === ADD_SLICEX) {
        let newState = JSON.parse(JSON.stringify(state))
        newState.slicex = action.value
        return newState
    }
    if (action.type === ADD_SLICEY) {
        let newState = JSON.parse(JSON.stringify(state))
        newState.slicey = action.value
        return newState
    }
    if (action.type === SLICE_CHANGE) {
        let newState = JSON.parse(JSON.stringify(state))
        newState.strslice = newState.str.slice(newState.slicex, newState.slicey)
        return newState
    }
    return state
}